﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JBeamLibrary.VehicleParts;

namespace DriveBeamEditor.Controls.BeamNG.VehiclePartConfig
{
    public partial class VehiclePartGlobal : VehiclePartControl
    {
        #region CTORs

        public VehiclePartGlobal()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void btSelectRefNode_SelectedNodeChanged(object sender, JBeamLibrary.VehicleParts.Elements.Node e)
        {
            if (VehiclePart != null)
                VehiclePart.RefNodes.RefNodeId = e != null ? e.Id : string.Empty;
            vehiclePartSource.ResetBindings(false);
        }

        private void btSelectBackNode_SelectedNodeChanged(object sender, JBeamLibrary.VehicleParts.Elements.Node e)
        {
            if (VehiclePart != null)
                VehiclePart.RefNodes.BackNodeId = e != null ? e.Id : string.Empty;
            vehiclePartSource.ResetBindings(false);
        }

        private void btSelectLeftNode_SelectedNodeChanged(object sender, JBeamLibrary.VehicleParts.Elements.Node e)
        {
            if (VehiclePart != null)
                VehiclePart.RefNodes.LeftNodeId = e != null ? e.Id : string.Empty;
            vehiclePartSource.ResetBindings(false);
        }

        private void btSelectUpNode_SelectedNodeChanged(object sender, JBeamLibrary.VehicleParts.Elements.Node e)
        {
            if (VehiclePart != null)
                VehiclePart.RefNodes.UpNodeId = e != null ? e.Id : string.Empty;
            vehiclePartSource.ResetBindings(false);
        }

        private void xbRefactorNodes_Click(object sender, EventArgs e)
        {
            Vehicle.RefactorNodes();
            RefreshData();
        }

        #endregion

        #region Methods

        protected override void BindVehiclePart()
        {
            kvcScaleBeamSpring.DataBindings.Clear();
            kvcScaleBeamDamp.DataBindings.Clear();
            kvcScaleBeamDeform.DataBindings.Clear();
            kvcScaleBeamStrength.DataBindings.Clear();

            kvcScaleNodeWeight.DataBindings.Clear();

            kvcSelfCollisionThickness.DataBindings.Clear();
            kvcRemoteCollisionThickness.DataBindings.Clear();
            kvcZeroOutSelfCollision.DataBindings.Clear();

            kvcRefNodesRef.DataBindings.Clear();
            kvcRefNodesBack.DataBindings.Clear();
            kvcRefNodesLeft.DataBindings.Clear();
            kvcRefNodesUp.DataBindings.Clear();

            if (VehiclePart != null)
            {
                kvcScaleBeamSpring.DataBindings.Add("EditValue", vehiclePartSource, "BeamScales.SpringScale", false,
                    DataSourceUpdateMode.OnPropertyChanged);
                kvcScaleBeamDamp.DataBindings.Add("EditValue", vehiclePartSource, "BeamScales.DampScale", false,
                    DataSourceUpdateMode.OnPropertyChanged);
                kvcScaleBeamDeform.DataBindings.Add("EditValue", vehiclePartSource, "BeamScales.DeformScale", false,
                    DataSourceUpdateMode.OnPropertyChanged);
                kvcScaleBeamStrength.DataBindings.Add("EditValue", vehiclePartSource, "BeamScales.StrengthScale",
                    false,
                    DataSourceUpdateMode.OnPropertyChanged);

                kvcScaleNodeWeight.DataBindings.Add("EditValue", vehiclePartSource, "NodeScales.WeightScale", false,
                    DataSourceUpdateMode.OnPropertyChanged);

                kvcSelfCollisionThickness.DataBindings.Add("EditValue", vehiclePartSource,
                    "Collisions.SelfCollisionThickness", false, DataSourceUpdateMode.OnPropertyChanged);
                kvcRemoteCollisionThickness.DataBindings.Add("EditValue", vehiclePartSource,
                    "Collisions.RemoteCollisionThickness", false, DataSourceUpdateMode.OnPropertyChanged);
                kvcZeroOutSelfCollision.DataBindings.Add("EditValue", vehiclePartSource,
                    "Collisions.ZeroOutSelfCollision", false, DataSourceUpdateMode.OnPropertyChanged);

                kvcRefNodesRef.DataBindings.Add("EditValue", vehiclePartSource, "RefNodes.RefNodeId", false,
                    DataSourceUpdateMode.OnPropertyChanged);
                kvcRefNodesBack.DataBindings.Add("EditValue", vehiclePartSource, "RefNodes.BackNodeId", false,
                    DataSourceUpdateMode.OnPropertyChanged);
                kvcRefNodesLeft.DataBindings.Add("EditValue", vehiclePartSource, "RefNodes.LeftNodeId", false,
                    DataSourceUpdateMode.OnPropertyChanged);
                kvcRefNodesUp.DataBindings.Add("EditValue", vehiclePartSource, "RefNodes.UpNodeId", false,
                    DataSourceUpdateMode.OnPropertyChanged);
            }

            base.BindVehiclePart();
        }

        protected override void OnVehiclePartChanged()
        {
            var refNode = VehiclePart != null ? Vehicle.FindNode(VehiclePart.RefNodes.RefNodeId) : null;
            var backNode = VehiclePart != null ? Vehicle.FindNode(VehiclePart.RefNodes.BackNodeId) : null;
            var leftNode = VehiclePart != null ? Vehicle.FindNode(VehiclePart.RefNodes.LeftNodeId) : null;
            var upNode = VehiclePart != null ? Vehicle.FindNode(VehiclePart.RefNodes.UpNodeId) : null;

            btSelectRefNode.VehiclePart = VehiclePart;
            btSelectBackNode.VehiclePart = VehiclePart;
            btSelectLeftNode.VehiclePart = VehiclePart;
            btSelectUpNode.VehiclePart = VehiclePart;

            btSelectRefNode.SelectedNode = refNode;
            btSelectBackNode.SelectedNode = backNode;
            btSelectLeftNode.SelectedNode = leftNode;
            btSelectUpNode.SelectedNode = upNode;

            base.OnVehiclePartChanged();
        }

        #endregion
    }
}
